package com.server.server;

import com.server.constant.CommonConstant;
import com.server.server.initializer.WebSocketTalkInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Description: This is a sample class.
 * @Author Simon
 */
public class WebSocketNettyServer {
    public static void start() {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        WebSocketTalkInitializer webSocketTalkInitializer = new WebSocketTalkInitializer();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(webSocketTalkInitializer);

            ChannelFuture channelFuture = serverBootstrap.bind(CommonConstant.WEBSOCKET_LISTEN_PORT).sync();

            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
